package com.yonyou.cyxdms.basedata.service.controller.brand;

import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.basedata.client.domains.vo.BrandCacheVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.VehicleVO;
import com.yonyou.cyxdms.basedata.service.service.brand.BrandService;
import com.yonyou.cyxdms.basedata.service.service.model.ModelService;
import com.yonyou.cyxdms.basedata.service.service.series.SeriesService;
import com.yonyou.cyxdms.basedata.service.vo.CarbrandInfoVO;
import com.yonyou.cyxdms.basedata.service.vo.brand.BrandDictVO;
import com.yonyou.cyxdms.basedata.service.vo.model.ModelDictVO;
import com.yonyou.cyxdms.basedata.service.vo.series.SeriesDictVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌
 *
 * @author: renwd
 * @since: 2018/12/7
 */

@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private ModelService modelService;

    @Autowired
    private SeriesService seriesService;

    //日志对象
    public static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    /**
     * 按条件查询品牌信息
     *
     * @param queryParam 条件
     * @return java.util.List<com.yonyou.cyxdms.basedata.client.domains.DTO.basedata.BrandCacheVO>
     * @author renwd
     * @since 2018/12/10
     */
    @GetMapping(value="/queryBrandByCondition")
    public List<BrandCacheVO> queryBrandByCondition(@RequestParam Map<String, Object> queryParam) {
        return brandService.queryBrandByCondition(queryParam);
    }

    /**
     * 根据DEALER_CODE查询所有的品牌信息
     *
     * @param
     * @return java.util.List<com.yonyou.cyxdms.basedata.client.domains.DTO.basedata.BrandCacheVO>
     * @author guorp
     * @since 2018/12/13
     */
    @GetMapping(value = "/getBrand")
    public List<BrandCacheVO> getBrand(){
        return  brandService.getBrand();
    }

    /**
     * //根据经销商id获取经销商品牌列表
     *
     * @author dengqiang
     * @since 2019/1/7
     */
    @GetMapping("/carbrandList")
    @ApiOperation(notes = "品牌列表", value = "品牌列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dealerId", required = true, dataType = "Long", paramType = "header", value = "经销商id"),
            @ApiImplicitParam(name = "appId", required = true, dataType = "String", paramType = "header", value = "租户ID"),
    })
    public List<CarbrandInfoVO> queryCarbrandInfo(@RequestHeader("appId") String appId,
                                                  @RequestHeader("dealerId") Long dealerId) {
        return brandService.queryCarbrandInfo(appId);
    }


    /**
     * @author     ：quankk
     * @date       ：2019/7/19 17:28
     * @description：
     */
    @GetMapping(value = "/queryBrand2")
    public List<Map> queryBrand2(@RequestParam Map<String, Object> queryParam){
        return  brandService.queryBrand2(queryParam);
    }

    /**长城映射数据
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/9/10
     */
    @GetMapping(value = "/queryBrandByAppidAndName")
    public Long queryBrandByAppidName(@RequestParam("appId") String appId,
                                      @RequestParam("brandName") String brandName) {
        return brandService.queryBrandByAppidName(appId, brandName);
    }


    /**
     * JK获取id与code映射数据
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/9/10
     */
    @GetMapping("/queryBrandMapping")
    public Map<Long, String> queryBrandMapping(@RequestParam("appId") String appId) {
        return brandService.queryBrandMapping(appId);
    }

    /**
     * 根据条件查询品牌，车系，车型，配置id列表
     *
     * @param searchCondition
     * @return com.yonyou.cyxdms.basedata.service.vo.VehicleVO
     * @author dengqiang
     * @since 2019/9/17
     */
    @GetMapping(value = "/getIdListByCondition")
    public VehicleVO getIdListByCondition(@RequestParam("searchCondition") String searchCondition){
        return brandService.getIdListByCondition(searchCondition);
    }

    /**
     * 未登陆获取所有品牌
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/11/4
     */
    @GetMapping("/getAllBrandByAppId")
    public List<BrandCacheVO> getAllBrandByAppId(@RequestParam("appId") String appId) {
        return this.brandService.getAllBrandByAppId(appId);
    }



    @GetMapping("/getProductBrandTree")
    public List<BrandDictVO> getProductTree() {
        List<BrandDictVO> brandDictVos = brandService.queryBrandDictAllNopar();
        List<SeriesDictVO> seriesDictVos=seriesService.querySeriesDictAll();
        List<ModelDictVO> modelDictVos=modelService.queryModelDictAll();
        List<BrandDictVO> collect1 = brandDictVos.stream().peek(b -> {
            List<SeriesDictVO> collect = seriesDictVos.stream().filter(s -> s.getBrandId().equals(b.getBrandId())).collect(Collectors.toList());
            List<SeriesDictVO> collect3 = collect.stream().peek(s -> {
                List<ModelDictVO> collect2 = modelDictVos.stream().filter(m -> m.getSeriesIntId().equals(s.getSeriesId())).collect(Collectors.toList());
                s.setChild(collect2);
            }).collect(Collectors.toList());
            b.setChild(collect3);
        }).collect(Collectors.toList());
        return collect1;
    }
    @GetMapping("/queryBrandDictAllNopar")
    public List<BrandDictVO> queryBrandDictAllNopar() {
        return brandService.queryBrandDictAllNopar();
    }
    @GetMapping("/querySeriesDictAll")
    public List<SeriesDictVO> querySeriesDictAll() {
        return seriesService.querySeriesDictAll();
    }
    @GetMapping("/queryModelDictAll")
    public List<ModelDictVO> queryModelDictAll() {
        return modelService.queryModelDictAll();
    }
}
